package com.gxa.controller.lgr.fileUpLoad;

import com.gxa.common.R;
import com.gxa.utils.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


@Controller
public class UploadAPI {

    //文件上传示例

    /**
     * 药品
     * @param multipartFile
     * @param request
     * @return
     */
    @PostMapping("/upload/drug")
    @ResponseBody
    public R uploadFile(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request) {
        String s = FileUtils.saveImage(multipartFile, request, FileUtils.DRUG_AVATAR);
        return R.ok(s);
    }

    @PostMapping("/upload/user_avatar")
    @ResponseBody
    public R uploadUserFile(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request) {
        String s = FileUtils.saveImage(multipartFile, request, FileUtils.USER_AVATAR);
        return R.ok(s);
    }

    //文件更新示例  注意旧文件路径一定要从数据库中拿到 /upload开头的数据
    @PostMapping("/upload/update")
    @ResponseBody
    public R updateImgFile(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        String Old = "/upload/images/user_avatar/982b1dc1-cf03-4a8f-9fa8-8e5a21b8c568_default_avater.jpg";
        String s = FileUtils.updateImgFile(file, request, Old, FileUtils.USER_AVATAR);
        return R.ok(s);
    }
}
